/*******************************************************************************
 *  myStorage.js, version 1.0.0
 *  (c) 2008 Sahid Ferdjaoui
 *
 *  myStorage.js is freely distributable under the terms of an GPL v3 license.
 *
 *******************************************************************************/

var myStorage = Class.create ({
	
	initialize: function () {
		this.Version = '1.0.0';
		this.h = new Hash ();
	},

	add: function (key, value, expire) {
		if (!expire)
			expire = 0;
		else
			expire += this.time ();

		if (this.get (key))
			return false;
		
		var atom = {'data':value, 'expire':expire};
		this.h.set (key, atom);
	},

	set: function (key, value, expire) {
		if (!expire)
			expire = 0;
		else
			expire += this.time ();
		
		var atom = {'data':value, 'expire':expire};
		this.h.set (key, atom);
	},

	get: function (key) {
		var atom = this.h.get (key);
		if (!atom)
			return false;
		if (atom.expire < this.time ())	{
			this.h.unset (key);
			return false;
		}
		return atom.data;
	},

	del: function (key) {
		this.h.unset (key);
	},

	time: function () {
		return Math.floor ((new Date()).getTime() / 1000);
	}
	
});